/*
 * Copyright (C) 2019-2020 Alibaba Group Holding Limited
 */
#ifndef _APP_MAIN_H_
#define _APP_MAIN_H_

#include <hw_config.h>
#include <app_config.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include <aos/aos.h>
#include <aos/cli.h>
#include <aos/cli_cmd.h>
#include <vfs.h>

#include <pin_name.h>
#include <drv/adc.h>
#include <drv/gpio.h>
#include <pinmux.h>
#include <devices/devicelist.h>
#include <media.h>
#include <mediaplayer.h>
#if defined(CONFIG_LCD_ILI9488) && CONFIG_LCD_ILI9488
#include <lcd_ili9488.h>
#endif

#include <yoc/yoc.h>
#include <yoc/eventid.h>
#include <yoc/uservice.h>

#include <clock_alarm.h>
#include <rtc_alarm.h>

#include "audio_res.h"
#include "app_aui_cloud.h"
#include "app_sys.h"

/**
 * csi sys medaly declare
 * 
 * @param ms delay time ms
 */
extern void mdelay(uint32_t ms);

/*************
 * init
 ************/
/**
 * yoc init
 */
void yoc_base_init(void);

/**
 * soft vad init
 */
int app_softvad_init(void);

/**
 * fct test init
 */
void fct_case_init(void);

/**
 * button init
 */
void app_button_init(void);

/**
 * clock alarm init
 */
void app_clock_alarm_init(void);

/**
 * set pa mute gpio
 *
 * @param[in] mute 1: set mute 0:cancel mute
 */
void app_speaker_mute(int mute);

/*************
 * mic
 ************/
/**
 * get mic busy status
 *
 * @return  1: busy 0: idle
 */
int app_mic_get_busy(void);

/**
 * set mic busy status
 *
 * @param[in] busy: 0 - not busy, 1 - busy
 */
void app_mic_set_busy(int busy);

/*************
 * gpio
 ************/
/**
 * get gpio value
 *
 * @param[in]  pin pinname defined by csi
 * 
 * @param[out] val get pin level
 *
 * @return  0: success
 */
int app_gpio_read(int pin, int *val);

/**
 * set gpio value
 *
 * @param[in]  pin pinname defined by csi
 * 
 * @param[out] val pin level(0 or 1)
 *
 * @return  0: success
 */
int app_gpio_write(int pin, int val);

/*************
 * lpm
 ************/
/**
 * init lpm system
 *
 * @return  void
 */
void app_lpm_init(void);

/**
 * update system active time tag for lpm check
 *
 * @return  void
 */
void app_lpm_update(void);

/**
 * get lpm status
 *
 * @return  1: can enter low power mode
 */
int app_lpm_check(void);


/*************
 * system led status 
 ************/
#define LED_TURN_OFF    0
#define LED_LOADING     1
#define LED_NET_DOWN    2
#define LED_NET_READY   3
#define LED_PLAYING     4
#define LED_NET_CONFIG  5
#define LED_TALKING     6
#define LED_IDLE        7
#define LED_LPM         8
#define LED_STATE_NUM   9

/**
 * set led status
 *
 * @param[in] state LED_TURN_OFF~LED_IDLE
 *
 * @return void
 */
void app_set_led_state(int state);

/**
 * set led control enable
 *
 * @param[in] en 1: enable, 0: disable
 *
 * @return void
 */
void app_set_led_enabled(int en);

/**
 * app status init, subscribe EVENT_UPDATE_STATUS
 *
 * @return void
 */
void app_status_init(void);

/**
 * publish status update event(EVENT_UPDATE_STATUS)
 *
 * @return void
 */
void app_status_update(void);

/*************
 * pwm led
 ************/
#define PWM_LED_LIGHT_MIN 10
#define PWM_LED_LIGHT_MAX 100

/**
 * pwm led init
 *
 * @return 0: sucess
 */
int8_t app_pwm_led_init();

/**
 * pwm led control
 *
 * @param[in] flag 1: turn on, 0: turn off
 *
 * @return 0: sucess
 */
int8_t app_pwm_led_control(const uint8_t flag);

/**
 * pwm led set brightness
 *
 * @param[in] brightness  PWM_LED_LIGHT_MIN~PWM_LED_LIGHT_MAX
 *
 * @return 0: success
 */
int8_t app_pwm_led_set_brightness(int32_t brightness);

/**
 * pwm led get current brightness
 *
 * @return current brightness
 */
int8_t app_pwm_led_get_brightness();

/**
 * pwm led get on/off status
 *
 * @return 0: off 1: on
 */
int8_t app_pwm_led_get_status();

/*************
 * Smart Living
 ************/
/**
 * control smartliving client
 *
 * @param[in]  flag          0: stop, others: start
 *
 * @return  0: success
 */
int smartliving_client_control(const int flag);

/**
 * Get connection status of smartliving client
 *
 * @return 0: disconnected
 */
int smartliving_client_is_connected(void);

/**
 * report property if changed for demo(pwm led status)
 *
 * @return void
 */
void user_post_property(void);

/**
 * report property of the key number pressed for demo
 *
 * @param[in]  key_num       Key Number
 */
void user_post_key_press_event(int key_num);

/*************
 * system except
 ************/
/**
 * system except process init
 */
void sys_event_init(void);

/*************
 * wifi & net 
 ************/
/* wifi provisioning method */
#define WIFI_PROVISION_SOFTAP           1
#define WIFI_PROVISION_SL_SMARTCONFIG   0
#define WIFI_PROVISION_SL_DEV_AP        3

typedef enum {
    MODE_WIFI_TEST = -2,
    MODE_WIFI_CLOSE = -1,
    MODE_WIFI_NORMAL = 0,
    MODE_WIFI_PAIRING = 1
}wifi_mode_e;
/**
 * network init
 *
 * @return wifi mode
 */
wifi_mode_e app_network_init(void);

/**
 * get wifi connect status
 *
 * @return 1: connnecting
 */
int wifi_connecting();

/**
 * set wifi low power enable
 *
 * @param [in] lpm_en 1:can enter low power mode, 0: cannot enter low power mode
 * 
 * @return 1: connnecting
 */
void wifi_lpm_enable(int lpm_en);

/**
 * set wifi low power
 *
 * @param [in] lpm_on 1:lpm on, 0:lpm off
 */
void wifi_set_lpm(int lpm_on);

/**
 * start wifi pair
 */
void wifi_pair_start(void);

/**
 * stop wifi pair
 */
void wifi_pair_stop(void);

/**
 * get internet connect status
 * 
 * @return 1: connected, 0: not connected
 */
int wifi_internet_is_connected();

/**
 * get wifi pairing status
 * 
 * @return 1: pairing, 0: not pairing
 */
int wifi_is_pairing();

/**
 * get mac address
 * 
 * @param [out] mac address buffer
 * 
 * @return 0: success
 */
int wifi_getmac(uint8_t mac[6]);

/*************
 * fota
 ************/
/**
 * fota init
 */
void app_fota_init(void);

/**
 * start fota task
 */
void app_fota_start(void);

/**
 * get fota download status
 * 
 * @return 1: downloading 0: normal
 */
int app_fota_is_downloading(void);

/**
 * force fota check
 */
void app_fota_do_check(void);

/**
 * start fota task
 */
void app_fota_set_auto_check(int enable);


/*************
 * USER EVENT LIST
 ************/
/* event id define */
#define EVENT_NTP_RETRY_TIMER       (EVENT_USER + 1)
#define EVENT_NET_CHECK_TIMER       (EVENT_USER + 2)
#define EVENT_NET_NTP_SUCCESS       (EVENT_USER + 3)
#define EVENT_NET_LPM_RECONNECT     (EVENT_USER + 4)

/*app_status_update 函数发送该消息，用于设备状态变化后更新LED灯 */
#define EVENT_UPDATE_STATUS (EVENT_USER + 10)

#define EVENT_LPM_CHECK     (EVENT_USER + 11)
#define EVENT_BATTERY_CHECK (EVENT_USER + 12)

/* media event */
#define EVENT_MEDIA_START           (EVENT_USER + 21)
#define EVENT_MEDIA_MUSIC_FINISH    (EVENT_USER + 22)
#define EVENT_MEDIA_SYSTEM_FINISH   (EVENT_USER + 23)
#define EVENT_MEDIA_MUSIC_ERROR     (EVENT_USER + 24)
#define EVENT_MEDIA_SYSTEM_ERROR    (EVENT_USER + 25)

/* 命令行测试 */
void cli_reg_cmd_aui(void);
void cli_reg_cmd_yvdbg(void);
void cli_reg_cmd_app(void);
void cli_reg_cmd_gpio(void);

#endif
